package com.shockweb.common.context;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.shockweb.common.context.ContextParam;

/**
 * 通用ThreadLocal管理工具
 * @author  彭明华
 * 2017年6月12日
 */
public class ContextManager {
    
	/**
	 * 请求的UUID
	 */
    public static final String UUID = "UUID";
    
    
	/**
	 * 请求的执行步骤
	 */
    public static final String STEP = "STEP";
    /**
     * 所有者
     */
    public static final String OWNER = "OWNER";
    /**
     * cache名
     */
    public static final String CACHE = "ThreadLocalUtil_CACHE";

    /**
     * 当前线程
     */
	private static final ThreadLocal<Map<String,Object>> threadLocal = 
	        new ThreadLocal<Map<String,Object>>(){
	        @Override 
	        protected Map<String,Object> initialValue() { 
	        	Map<String,Object> map = new HashMap<String,Object>();
	        	map.put(CACHE, new HashMap<String,Object>());
	            return map; 
	        }};
	        
	        
			
	/**
	 * 删除ThreadLocal
	 */
	public static void remove(){
		threadLocal.remove();
	}
	/**
	 * 获取缓存
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Map<String,Object> getCache(){
		if(threadLocal==null){
	    	Map<String,Object> map = new HashMap<String,Object>();
	    	map.put(CACHE, new HashMap<String,Object>());
	    	threadLocal.set(map);
		}
		return (HashMap<String,Object>)threadLocal.get().get(CACHE);
	}
	        
	/**
	 * 获取当前线程的上下文参数
	 * @return
	 */
	public static ContextParam getContextParam(){
		ContextParam param = new ContextParam(getUuid());
		param.putAll(threadLocal.get());
		//不复制CACHE
		param.remove(CACHE);
		return param;
	}
	

	     
	/**
	 * 获取当前线程的上下文参数,带所有者
	 * @param owner
	 * @return
	 */
	public static ContextParam getContextParam(String owner){
		ContextParam param = new ContextParam(getUuid(),owner);
		param.putAll(threadLocal.get());
		//不复制CACHE
		param.remove(CACHE);
		return param;
	}
	
	/**
	 * 获取当前ThreadLocal的数据
	 * @param key
	 * @return
	 */
	public static Object getValue(String key){
	    return threadLocal.get().get(key); 
	}
	
	/**
	 * 删除当前ThreadLocal的数据
	 * @param key
	 * @return
	 */
	public static Object remove(String key){
		return threadLocal.get().remove(key);
	}
	
	/**
	 * 设置当前ThreadLocal的数据
	 * @param key
	 * @param value
	 */
	public static void putValue(String key,Object value){
		threadLocal.get().put(key, value);
	}
	
	/**
	 * 设置当前ThreadLocal的一批数据
	 * @param values
	 */
	public static void putAll(Map<String,Object> values){
		Iterator<String> it = values.keySet().iterator();
		while(it.hasNext()){
			String key = it.next();
			if(key.equals(STEP) && values.get(key) instanceof Long){
				putValue(key,(Long)values.get(key) + 1);
			}else{
				putValue(key,values.get(key));
			}
		}

	}
	

    /**
     * 设置uuid
     * @param uuid
     */
    public static void setUuid(String uuid){
    	threadLocal.get().put(UUID, uuid);
    }
    
    /**
     * 获取执行步骤
     * @return
     */
    public static Long getStep(){
        if(!threadLocal.get().containsKey(STEP)){
            threadLocal.get().put(STEP, new Long(0));
        }
        return (Long)threadLocal.get().get(STEP);
    }
    
    /**
     * 获取当次访问的唯一编号，由系统自动生成
     * @return
     */
    public static String getUuid(){
        if(!threadLocal.get().containsKey(UUID)){
            threadLocal.get().put(UUID, java.util.UUID.randomUUID().toString());
        }
        return (String)threadLocal.get().get(UUID);
    }
    
    /**
     * 获取所有者标识
     * @return
     */
    public static String getOwner(){
		if(threadLocal.get().get(OWNER)==null){
			return null;
		}else{
			return (String)threadLocal.get().get(OWNER);
		}
    }
}
